package com.yidiandao.business.ui.main.usercenter;

import com.yidiandao.business.data.bean.ShareEntity;
import com.yidiandao.business.data.bean.User;
import com.yidiandao.business.data.model.IMessageModel;
import com.yidiandao.business.data.model.IUserModel;
import com.yidiandao.business.data.model.MessageModel;
import com.yidiandao.business.data.model.UserModel;
import com.yidiandao.business.data.realm.BrowingHistoryBean;
import com.yidiandao.business.data.response.UnReadMessageResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

public class UserCenterPresenter implements UserCenterContract.Presenter {

    private final IUserModel mUserModel;

    private UserCenterContract.View mUserCenterView;

    private RealmResults<BrowingHistoryBean> mResults;

    private ShareEntity mShareEntity;

    private boolean isLoadingUserInfo;

    private boolean isGetBrowsing;

    private IMessageModel mMessageModel;

    private RealmChangeListener callback = new RealmChangeListener() {
        @Override
        public void onChange(Object element) {
            isGetBrowsing = false;
            mUserCenterView.setBrowsingHistoryNum(mResults.size());
        }
    };

    public UserCenterPresenter(UserCenterContract.View view) {
        mUserCenterView = view;
        mUserCenterView.setPresenter(this);

        mUserModel = new UserModel();
        mMessageModel = new MessageModel();
    }

    @Override
    public void start() {
        getUserInfo();
        getData();
        getTotalUnReadMessageCount();
    }

    private void getTotalUnReadMessageCount() {
        mMessageModel.getUnMessageCountFromServer(mUserCenterView.getPagerTag(),
                new IMessageModel.OnGetUnReadMessageCountListener() {
                    @Override
                    public void onSuccess(UnReadMessageResponse.UnReadMessage unReadMessage) {
                        int activityCount = unReadMessage.getActivityCount();
                        int assetCount = unReadMessage.getAssetCount();
                        int discoveryCount = unReadMessage.getDiscoveryCount();
                        int logisticsCount = unReadMessage.getLogisticsCount();
                        int systemCount = unReadMessage.getSystemCount();

                        int totalUnReadMessageCount = assetCount + logisticsCount + systemCount + activityCount + discoveryCount;
                        mUserCenterView.refreshTotalUnReadMessage(totalUnReadMessageCount);
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }

    private void getData() {
        if (isGetBrowsing)
            return;
        isGetBrowsing = true;
        try {
            Realm realm = mUserCenterView.getRealm();
            mResults = realm.where(BrowingHistoryBean.class)
                    .equalTo("userId", PreferenceUtils.getUserId(UIUtils.getContext()))
                    .findAllAsync();
            mResults.addChangeListener(callback);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Override
    public void getUserInfo() {
        if (isLoadingUserInfo)
            return;
        isLoadingUserInfo = true;
        mUserCenterView.showDialog();
        mUserModel.getPersonalInfo(mUserCenterView.getPagerTag(), new IUserModel.OnGetPersonalInfoListener() {
            @Override
            public void onSuccess(User user) {

                setPersonalData(user);
            }

            @Override
            public void onFinal() {
                isLoadingUserInfo = false;
                mUserCenterView.disMissDialog();
            }
        });
    }

    @Override
    public void destroy() {
        mResults.removeChangeListener(callback);
    }

    @Override
    public boolean hasNewAgentPushMessage() {
        return mUserModel.hasNewAgentPushMessage();
    }

    @Override
    public void setHasNewAgentPushMessage(boolean hasNewAgentMessage) {
        mUserModel.setHasNewAgentPushMessage(hasNewAgentMessage);
    }

    @Override
    public void setHasNewOfferMessage(boolean hasNewOfferMessage) {
        mUserModel.setHasNewOfferMessage(hasNewOfferMessage);
    }

    @Override
    public boolean hasNewOfferMessage() {
        return mUserModel.hasNewOfferMessage();
    }

    private void setPersonalData(User user) {
        // 保存用户昵称、性别、头像，联系客服的使用
        PreferenceUtils.setSex(UIUtils.getContext(), user.getSex());
        PreferenceUtils.setNickName(UIUtils.getContext(), user.getNickName());

        mUserCenterView.setUserInfo(user);
        mShareEntity = user.getShare();
    }

    @Override
    public ShareEntity getShareData() {
        return mShareEntity;
    }
}
